package org.ccay.web.support.internal.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.log.CcayLoggerFactory;
import org.ccay.core.log.ILogger;
import org.ccay.core.request.IRequestContext;
import org.ccay.core.request.impl.RequestContext;
import org.ccay.core.util.LanguageUtil;
import org.ccay.registry.util.RegistryQueryUtil;
import org.ccay.security.authc.DefaultProgramAuthorizationInfo;
import org.ccay.security.reaml.Reaml;
import org.ccay.sitemap.SiteNodeSimpleVO;
import org.ccay.sitemap.SiteNodeVO;
import org.ccay.sitemap.service.ISiteMapQueryService;
import org.ccay.web.support.WorkspaceVO;
import org.ccay.web.support.service.IHomeService;

@Named("homeService")
public class HomeService implements IHomeService {

	/**
	 * 菜单服务
	 */
	@Inject
	private ISiteMapQueryService siteMapService;
	
	@Inject 
	private Reaml realm;
	
	private static final ILogger log = CcayLoggerFactory.getLogger(IHomeService.class);
	
	@Override
	public WorkspaceVO findWorkspaceInfo(boolean nomenu) throws ApplicationException {
		WorkspaceVO workspace = new WorkspaceVO();
		
		IRequestContext context = RequestContext.getCurrent();
		workspace.setUser(context.getUser());
		workspace.setEnvBean(RequestUtil.getEnvBeanByNetwrok());
		
		workspace.setSwitchThemeEnabled(RegistryQueryUtil.getBooleanValue("Ccay.GlobalParamters.ThemeSwitch") ? "1" : "0");

		// TODO 用户个性化
		//workspace.setUserSettingList(getPersonalizeSettings(context.getUser().getUserId()));
		// 当前主题名
		//workspace.setThemeName((String) context.getSession().get("CCAY5_THEME"));
		//根据nomenu参数判断是否提供左侧菜单所有数据
		if(nomenu){
			//仅提供首页数据，用于加载首页
			SiteNodeVO fullMapRoot = siteMapService.findWholeSiteMap(RequestContext.getCurrent().getUserLanguage());
			workspace.setLeftMenuNode(new SiteNodeSimpleVO(fullMapRoot));
		}else{
			workspace.setLeftMenuNode(siteMapService.findAuthorizedSiteMap());
		}
		workspace.setSwitchLanguageEnabled(RequestLangUtil.isSwitchEnabled());
		workspace.setValidLanguages(RequestLangUtil.getValidLanguages());
		workspace.setCurrentLanguage(context.getUserLanguage());
		workspace.setContextPath(context.getApplication().getContextPath());
		// 服务器时时间
		workspace.setServerTime(new Date());
		//权限列表
		if(null != context.getUser()){
			PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
			DefaultProgramAuthorizationInfo authorizationInfo = (DefaultProgramAuthorizationInfo)realm.getCachedAuthorizationInfo(principals);
	        workspace.setPermissions(authorizationInfo.getStringPermissions());
	        workspace.setProgramRoles(authorizationInfo.getProgramRoles());
		}
        
		//多语言列表
		workspace.setLanguageList(getLanguageList());
		//add end;
		return workspace;
	}
	
	
	
	/**
	 * 获取多语言列表
	 * @return
	 * @throws ApplicationException 
	 */
	public List<Map<String, String>> getLanguageList() throws ApplicationException{
		//值：en_US,zh_CN,de_DE,ko_KR,fr_FR
		String validLanValueStr = RegistryQueryUtil.getValue("Ccay.GlobalParamters.ValidLanguages");
		//显示名称：de_DE=Ge德语,ko_KR=ko초매,fr_FR=fr法
		String validLanDisplayStr = RegistryQueryUtil.getValue("Ccay.GlobalParamters.ValidLanguages.display");
		List<Map<String, String>> languageList = new ArrayList<Map<String, String>>();
		String[] validList = validLanValueStr!=null ? validLanValueStr.split(",") : new String[]{};
		String langugae = validLanDisplayStr!=null?validLanDisplayStr:"";
		for (String str : validList) {
			if(!str.equals("zh_CN") && !str.equals("en_US")){
				Map<String, String> map = new HashMap<String, String>();
				map.put("id", str);
				map.put("text",LanguageUtil.getLanguageStr(langugae, str));
			   languageList.add(map);
			}
		}
		return languageList;
	}
}